using DatabaseMigrationTool.Models;
using DatabaseMigrationTool.Services;
using Microsoft.AspNetCore.Mvc;

namespace DatabaseMigrationTool.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class DatabaseController : ControllerBase
    {
        private readonly DatabaseMigrationService _migrationService;
        private readonly ILogger<DatabaseController> _logger;

        public DatabaseController(
            DatabaseMigrationService migrationService,
            ILogger<DatabaseController> logger)
        {
            _migrationService = migrationService;
            _logger = logger;
        }

        [HttpGet("tables")]
        public async Task<IActionResult> GetTables([FromQuery] DatabaseConnectionInfo connectionInfo)
        {
            try
            {
                var tables = await _migrationService.GetTablesAsync(connectionInfo);
                return Ok(tables);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取表列表失败");
                return StatusCode(500, new { message = "获取表列表失败", error = ex.Message });
            }
        }

        [HttpGet("foreignKeys")]
        public async Task<IActionResult> GetForeignKeys([FromQuery] DatabaseConnectionInfo connectionInfo, [FromQuery] List<string> tables)
        {
            try
            {
                var foreignKeys = await _migrationService.GetForeignKeysForTablesAsync(connectionInfo, tables);
                return Ok(foreignKeys);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取外键关系失败");
                return StatusCode(500, new { message = "获取外键关系失败", error = ex.Message });
            }
        }

        [HttpPost("foreignKeys")]
        public async Task<IActionResult> GetForeignKeysPost([FromBody] ForeignKeyRequest request)
        {
            try
            {
                var foreignKeys = await _migrationService.GetForeignKeysForTablesAsync(request.ConnectionInfo, request.Tables);
                return Ok(foreignKeys);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取外键关系失败: " + ex.ToString());
                return StatusCode(500, new { message = "获取外键关系失败", error = ex.Message });
            }
        }

        [HttpPost("migrate")]
        public async Task<IActionResult> Migrate([FromBody] DatabaseMigrationRequest request)
        {
            try
            {
                var result = await _migrationService.MigrateAsync(request);
                return Ok(new { success = result, message = "数据库迁移完成" });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "数据库迁移失败");
                return StatusCode(500, new { message = "数据库迁移失败", error = ex.Message });
            }
        }

        [HttpPost("testConnection")]
        public async Task<IActionResult> TestConnection([FromBody] DatabaseConnectionInfo connectionInfo)
        {
            try
            {
                // 验证连接是否可用
                var tables = await _migrationService.GetTablesAsync(connectionInfo);
                return Ok(new { success = true, message = "连接成功", tables });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "数据库连接测试失败");
                return StatusCode(500, new { success = false, message = "连接失败", error = ex.Message });
            }
        }
    }
} 